﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Ninject;
using WPF_CAD.Common;
using WPF_CAD.Strategy;
using System.Collections.Generic;
using WPF_CAD.Model;

namespace WPF_CAD.ViewModel
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        #region Members

        public IStrategy Strategy { get; private set; }

        private IList<IFigure> _drawedFigures;

        public IList<IFigure> DrawedFigures
        {
            get { return _drawedFigures; }
            private set 
            {
                _drawedFigures = value;
                InvokePropertyChanged("DrawedFigures");
            }
        }

        private IFigure _selectedFigure;

        public IFigure SelectedFigure
        {
            get { return _selectedFigure; }
            set
            {
                _selectedFigure = value;
                InvokePropertyChanged("SelectedFigure");
            }
        }

        private readonly IKernel _kernel;

        #endregion

        #region Commands
        /// <summary>
        /// Comando para cambiar la estrategia actual a la LineToolStrategy
        /// </summary>
        public ICommand LineToolCommand
        {
            get { return new DelegateCommand(ChangeStrategyToLineToolStrategy); }
        }

        /// <summary>
        /// Comando para cambiar la estrategia actual a la PointToolStrategy
        /// </summary>
        public ICommand PointToolCommand
        {
            get { return new DelegateCommand(ChangeStrategyToPointToolStrategy); }
        }

        public ICommand PolyLineToolCommand 
        {
            get { return new DelegateCommand(ChangeStrategyToPolyLineToolStrategy); }
        }

        public ICommand SelectionToolCommand
        {
            get { return new DelegateCommand(ChangeStrategyToSelectionToolStrategy); }
        }

        #endregion

        #region Public Methods
        
        public void AddFigure(IFigure drawedFigure)
        {
            if (_drawedFigures.Contains(drawedFigure))
            {
                _selectedFigure = drawedFigure;
                return;
            }

            if (drawedFigure.DrawStatus.Equals(DrawStatus.Completed))
                DrawedFigures.Add(drawedFigure);
        }

        #endregion

        #region Private Methods
        
        private void ChangeStrategyToPointToolStrategy()
        {
            Strategy = _kernel.Get<PointToolStrategy>();
        }

        private void ChangeStrategyToLineToolStrategy()
        {
            Strategy = _kernel.Get<LineToolStrategy>();
        }

        private void ChangeStrategyToPolyLineToolStrategy()
        {
            Strategy = _kernel.Get<PolyLineToolStrategy>();
        }

        private void ChangeStrategyToSelectionToolStrategy()
        {
            Strategy = _kernel.Get<SelectionToolStrategy>();
        }

        #endregion
        
        #region Constructors
        /// <summary>
        /// Contructor with Injected Strategy
        /// </summary>
        /// <param name="stategy">Es la estrategia que representa la herramienta en uso</param>
        public MainWindowViewModel(IStrategy stategy)
        {
            Strategy      = stategy;
            _kernel        = new StandardKernel(new CadModule());
            DrawedFigures = new List<IFigure>();
        }

        #endregion

        #region INotifyPropertyChanged
        
        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        } 

        #endregion

        
    }
}
